package com.asm.xxljob.controller;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class HelloController {

    @Value("${server.port}")
    private int port;

    @XxlJob("demoJobHandler")
    public String helloJob(){
        System.out.println("简单任务执行:"+port);
        return "hello xxl-job";
    }

    @XxlJob("shardingJobHandler_asm")
    public void shardingJob(){

        // 分片参数
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        XxlJobHelper.log("分片参数：当前分片序号 = {}, 总分片数 = {}", shardIndex, shardTotal);
        List<Integer> list=getList();
        // 业务逻辑
        for (Integer integer : list) {
            if(integer%shardTotal==shardIndex){
                System.out.println("我是分片"+shardIndex+"，我正在处理"+integer);
            }

        }

    }

    private List<Integer> getList() {
        List<Integer> list=new ArrayList<>();
        for(int i=0;i<10000;i++){
            list.add(i);
        }
        return list;
    }
}
